<xsl:stylesheet version="1.1"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:math="http://exslt.org/math"
	xmlns="http://www.w3.org/2000/svg" 
	extension-element-prefixes="math">

	<xsl:import href="../../stylesheet.xslt" />
	<xsl:import href="humanoid.xslt" />

	<xsl:output method="xml" />

	<xsl:param name="orientation"/>	
	<xsl:param name="state"/>	

	<xsl:template match="/">

		<xsl:call-template name="output-stylesheet-declaration" />
		
		<xsl:variable name="minx">
			<xsl:apply-templates select="part" mode="get-extent">
				<xsl:with-param name="direction" select="'west'"/>
			</xsl:apply-templates>
		</xsl:variable>

		<xsl:variable name="maxx">
			<xsl:apply-templates select="part" mode="get-extent">
				<xsl:with-param name="direction" select="'east'"/>
			</xsl:apply-templates>
		</xsl:variable>

		<xsl:variable name="miny">
			<xsl:apply-templates select="part" mode="get-extent">
				<xsl:with-param name="direction">north</xsl:with-param>
			</xsl:apply-templates>
		</xsl:variable>

		<xsl:variable name="maxy">
			<xsl:apply-templates select="part" mode="get-extent">
				<xsl:with-param name="direction">south</xsl:with-param>
			</xsl:apply-templates>
		</xsl:variable>
		
		<xsl:variable name="grabx">
			<xsl:apply-templates select="part" mode="get-extent">
				<xsl:with-param name="direction">grabx</xsl:with-param>
			</xsl:apply-templates>
		</xsl:variable>

		<xsl:variable name="graby">
			<xsl:apply-templates select="part" mode="get-extent">
				<xsl:with-param name="direction">graby</xsl:with-param>
			</xsl:apply-templates>
		</xsl:variable>

		<xsl:variable name="width" select="$maxx - $minx"/>
		<xsl:variable name="height" select="$maxy - $miny"/>
		<xsl:variable name="x" select="$minx"/>
		<xsl:variable name="y" select="$miny"/>
		
		<svg width="{$width}" height="{$height}" viewBox="{$x} {$y} {$width} {$height}">
			<g>
				<xsl:apply-templates select="part" mode="render">
					<xsl:with-param name="orientation" select="$orientation"/>
					<xsl:with-param name="state" select="$state"/>
				</xsl:apply-templates>
				
				<circle id="grab-point" cx="{$grabx}" cy="{$graby}" r="1" fill="black" opacity="1"/>
				
			</g>

		</svg>
	</xsl:template>

</xsl:stylesheet>